using System;
using System.Collections.Generic;
using System.Text;
using ThaiLanguageTools.DataStruct;
using System.Text.RegularExpressions;

namespace ThaiLanguageTools.SrcParse
{
    class LoysBglParser : BabylonBglParser
    {
        const String DefStart = @"[0-9]\.+";
        const String HtmlTag = @"<.*?>";

        public LoysBglParser() :
            base("en", "th", Utils.ThaiEncoding, Utils.ThaiEncoding)
        {

        }

        protected override XDictEntry CreateEntry(string head, string defStr, 
            List<string> alternates)
        {            
            XDictEntry e = base.CreateEntry(head, null, alternates);

            String[] split = Regex.Split(defStr, @"<BR>");
            foreach(String s in split)
            {
                if (Regex.IsMatch(s, DefStart + ".*"))
                {
                    ProcessDef(e, s);
                }
                else
                {
                    CleanUpAndAdd(e.Comments, s, DstLang);
                }
            }

            return e;
        }

        bool IsEmpty(String s)
        {
            if (s == null) { return true; }
            return s.Trim().Length == 0;
        }

        void ProcessDef(XDictEntry e, String str)
        {
            str = Regex.Replace(str, DefStart, String.Empty);

            String def, examples;
            Utils.SplitInTwo(str, ":", out def, out examples);

            def = CleanUp(def);
            if (def.StartsWith("(grammar)") || 
                def.StartsWith("(trick)"))
            {
                // Isolate non-definition comments
                CleanUpAndAdd(e.Comments, def, DstLang);
            }
            else 
            {
                if (def.StartsWith("("))
                {
                    // Add straight if it's an explanation/similar
                    CleanUpAndAdd(e.Definitions, def, DstLang);
                }
                else
                {
                    // Split into multiple definitions
                    String[] defSpl = def.Split(',');
                    foreach (String ds in defSpl)
                    {
                        CleanUpAndAdd(e.Definitions, ds, DstLang);
                    }
                }
            }

            String[] exSpl = examples.Split(';');
            foreach (String ex in exSpl)
            {
                CleanUpAndAdd(e.Samples, ex, SrcLang);
            }
        }
        static String CleanUp(String str)
        {
            // Unescape HTML for easier viewing
            //def = def.Replace('<', '[');
            //def = def.Replace('>', ']');
            str = Regex.Replace(str, HtmlTag, String.Empty);
            str = str.Replace("&quot;", "\"");
            return str.Trim();
        }
        static void CleanUpAndAdd(List<XWordInfo> list, String str, String lang)
        {
            str = CleanUp(str);
            if (!String.IsNullOrEmpty(str))
            {
                list.Add(new XWordInfo(str, lang));
            }
        }
    }
}
